package aml.bs.commons;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
import java.util.Date;
import java.util.Objects;
import java.util.regex.Pattern;

/**
 * @Title: DateUtils
 * @Author duxuqiang02
 * @Date 2023/9/17 12:07
 * @description: 时间工具类
 */
public class DateUtil {

    //    时间格式应为：yyyy-MM-dd
    public static final String yyyyMMddRegStr = "((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))";

    // 时间格式应为：yyyy-MM-dd HH:mm:ss
    public static final String yyyyMMddHHmmssRegStr = "^((([0-9]{3}[1-9]|[0-9]{2}[1-9][0-9]{1}|[0-9]{1}[1-9][0-9]{2}|[1-9][0-9]{3})-(((0[13578]|1[02])-(0[1-9]|[12][0-9]|3[01]))|((0[469]|11)-(0[1-9]|[12][0-9]|30))|(02-(0[1-9]|[1][0-9]|2[0-8]))))|((([0-9]{2})(0[48]|[2468][048]|[13579][26])|((0[48]|[2468][048]|[3579][26])00))-02-29))\\s+([0-1]?[0-9]|2[0-3]):([0-5][0-9]):([0-5][0-9])$";

    public static final String yyyyMMddHHmmssStr = "yyyy-MM-dd HH:mm:ss";
    public static final String yyyyMMddStr = "yyyy-MM-dd";
    public static final String yyyyMMStr = "yyyy/MM/dd";
    public static final Pattern yyyyMMddReg = Pattern.compile(yyyyMMddRegStr);
    public static final Pattern yyyyMMddHHmmssRegReg = Pattern.compile(yyyyMMddHHmmssRegStr);


    /**
     * 获得当前日期 yyyy-MM-dd HH:mm:ss
     *
     * @return 2019-08-27 14:12:40
     */
    public static String getCurrentTime() {
        // 小写的hh取得12小时，大写的HH取的是24小时
        SimpleDateFormat df = new SimpleDateFormat(yyyyMMddHHmmssStr);
        Date date = new Date();
        return df.format(date);
    }

    /**
     * 获取系统当前时间戳
     *
     * @return 1566889186583
     */
    public static String getSystemTime() {
        return String.valueOf(System.currentTimeMillis());
    }

    /**
     * 获取当前日期 yy-MM-dd
     *
     * @return 2019-08-27
     */
    public static String getDateByString() {
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat(yyyyMMddStr);
        return sdf.format(date);
    }

    /**
     * 日期时间字符串转换为日期时间(java.time.LocalDateTime)
     *
     * @param localDateTimeStr 日期时间字符串
     * @param pattern          日期时间格式 例如DATETIME_PATTERN
     * @return LocalDateTime 日期时间
     */
    public static LocalDateTime parseLocalDateTime(String localDateTimeStr, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDateTime.parse(localDateTimeStr, dateTimeFormatter);
    }

    public static Date parseDateStrToDate(String dateStr, String pattern) {
        try {
            if (StringUtils.isBlank(dateStr)) {
                return null;
            }
            return Objects.requireNonNull(DateUtils.parseDate(dateStr, pattern));
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 判断是否匹配正则 2023-09-11
     *
     * @param dateStr
     * @return
     */
    public static Boolean parseDateStrWithYMD(String dateStr) {
        if (StringUtils.isBlank(dateStr)) {
            return false;
        }
        return dateStr.matches(yyyyMMddRegStr);
    }

    /**
     * 判断是否匹配正则 2023-09-11 00:00:00
     *
     * @param dateStr
     * @return
     */
    public static Boolean parseDateStrWithYMDHMS(String dateStr) {
        if (StringUtils.isBlank(dateStr)) {
            return false;
        }
        return dateStr.matches(yyyyMMddRegStr);
    }

    /**
     * 日期字符串转换为日期(java.time.LocalDate)
     *
     * @param localDateStr 日期字符串
     * @param pattern      日期格式 例如DATE_PATTERN
     * @return LocalDate 日期
     */
    public static LocalDate parseLocalDate(String localDateStr, String pattern) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(pattern);
        return LocalDate.parse(localDateStr, dateTimeFormatter);
    }

    /**
     * 102312314  -> 2023-02-01 00:00:00  时间戳10位:秒级别
     *
     * @param timeStamp
     * @return
     */
    public static Date parseTimeStampExcludeScale(Long timeStamp) {
        if (timeStamp == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(timeStamp));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime();
    }

    /**
     * 转化为字符串
     *
     * @param timeStamp
     * @return
     */
    public static String parseTimeStampExcludeScaleStr(Long timeStamp) {
        if (timeStamp == null) {
            return null;
        }
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date(timeStamp));
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTime().toString();
    }

    /**
     * 时间边时间戳
     *
     * @param date
     * @return
     */
    public static Long dateToTimeStamp(Date date) {
        return date.getTime();
    }


}
